﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartProduction.ErrorCode;
using SmartProduction.Write.API.Application.Commands.BasicCeate;
using SmartProduction.Write.API.Application.Commands.SCWorkCommand;

namespace SmartProduction.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WrokOrderController : ControllerBase
    {
        private readonly IMediator mediator;

        public WrokOrderController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 生产工单添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> SCWorkOrderAdd(SCWorkOrderAddCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 上传图片/文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadPic(IFormFile file)
        {
            try
            {
                var res = "wwwroot/img/" + file.FileName;
                using (FileStream fs = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:5182/img/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 质检添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> BasicCreate(ZhijianBasicinformationcreatecommand request)
        {
            try
            {

                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
